package scut.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import scut.result.CodeMsg;
import scut.result.Result;
import scut.util.IpUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController

public class LocationController {
    private final String KEY = "68451798da8efa325364c8ff384bc9d7";

    /**
     * 通过ip获取定位
     *
     * @param request
     * @return
     */
    @GetMapping("/ip")
    public Result<String> ipLocation(HttpServletRequest request) {

        String ip = IpUtil.getIpAddr(request);
        int type = IpUtil.isIpv4(ip) ? 4 : 6;
        String url = "https://restapi.amap.com/v5/ip?key=" + KEY + "&type=" + type + "&ip=" + ip;
        String data = getData(url);
        return Result.success(data);
    }


    @PostMapping("/map")
    public Result<String> getMap(@RequestBody Map<String, String> map) {
        String location = map.get("location");
        if (IpUtil.isEmpty(location) || IpUtil.isEmpty("zoom")) {
            return Result.error(CodeMsg.BIND_ERROR);
        }
        String zoom = map.get("zoom");
        String url = "https://restapi.amap.com/v3/staticmap?location=" + location + "&zoom=" + zoom + "&key=" + KEY;
        String data = getData(url);
        return Result.success(data);
    }

    /**
     * 将地址转化成经纬度等信息
     *
     * @param map
     * @return
     */
    @PostMapping("/transform")
    public Result<String> nameToLocation(@RequestBody Map<String, String> map) {
        String address = map.get("address");
        if (IpUtil.isEmpty(address)) {
//        Todo 返回错误信息
            return Result.error(CodeMsg.BIND_ERROR);
        }
        String city = map.getOrDefault("city", null);
        String url = "https://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=" + KEY;
        if (!IpUtil.isEmpty(city)) {
            url += "&city=" + city;
        }
        String data = getData(url);
        return Result.success(data);
    }

    private String getData(String url) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> getMessage = restTemplate.getForEntity(url, String.class);
        return getMessage.getBody();
    }

    /**
     * 将经纬度转成地址信息
     * @param map
     * @return
     */
    @PostMapping("/inverseTransform")
    public Result<String> locationToName(@RequestBody Map<String, String> map) {
        String location = map.get("location");
        String poitype = map.get("poitype");
        String radius = map.get("radius");
        if (IpUtil.isEmpty(location)) {
            return Result.error(CodeMsg.BIND_ERROR);
        }

        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + KEY + "&location=" + location;
        if (!IpUtil.isEmpty(poitype)) {
            url += "&poitype=" + poitype;
        }
        if (!IpUtil.isEmpty(radius)) {
            url += "&radius=" + radius;
        }
        String data = getData(url);
        return Result.success(data);
    }
}
